package com.example.app2

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController

class MainFragment : Fragment() {
    var myViewModel:MyViewModel?=null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        Log.e("MainFragment","onCreateView")
        val view = inflater.inflate(R.layout.fragment_main, container, false)
        myViewModel= ViewModelProvider(requireActivity()).get(MyViewModel::class.java)
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        view.findViewById<
                Button>(R.id.button).setOnClickListener {
            myViewModel?.changText("lizongjin...")
            //第一种
//            findNavController().navigate(R.id.detailFragment)
            // 第二种 通过action
            findNavController().navigate(R.id.action_mainFragment_to_detailFragment)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e("MainFragment","onDestroy")
    }
}